﻿using System.Data;
using System.Text;
using BusinessLogic.BusinessRules;
using DataAccess;
using Model.Data;

namespace BusinessLogic.ClassFactory
{
    /// <summary>
    /// SectionsFactory 的摘要说明。
    /// </summary>
    public class SectionsFactory : BaseFactory
    {
        public SectionsFactory()
            : base()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public SectionsFactory(DBManager l_DBManager)
            : base(l_DBManager)
        {
        }

        public Sections NewInstance()
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSections = new Sections(this.m_Manager, l_Access);
            objSections.NewDataSet();
            objSections.SelectRow(0);
            return objSections;
        }

        public Sections CreatorBySql(string Sql)
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSections = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryBySql(ref l_ds, Sql) < 1)
            {
                return null;
            }
            objSections.SetDataSet(l_ds);
            objSections.SelectRow(0);
            return objSections;
        }

        public Sections CreatorByFullSql(string Sql)
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSections = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryByQuerySql(ref l_ds, Sql) < 1)
            {
                return null;
            }
            objSections.SetDataSet(l_ds);
            objSections.SelectRow(0);
            return objSections;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="intSectionID"></param>
        /// <returns></returns>
        public Sections Creator(int intSectionID)
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSections = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryByID(ref l_ds, intSectionID) < 1)
            {
                return null;
            }
            objSections.SetDataSet(l_ds);
            objSections.SelectRow(0);
            return objSections;
        }

        public Sections Creator()
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSections = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryAll(ref l_ds) < 1)
            {
                return null;
            }
            objSections.SetDataSet(l_ds);
            objSections.SelectRow(0);
            return objSections;
        }

        //获取兄弟栏目数据
        public Sections CreatorByBrotherSectionID(int intSectionID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT * FROM [SECTIONS] WHERE intParentID=(SELECT intParentID FROM [SECTIONS] WHERE INTSECTIONID={0}) order by intSortNo desc,dtmInTime desc,intSectionID desc", intSectionID);
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSection = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryByQuerySql(ref l_ds, strSql.ToString()) < 1)
            {
                return null;
            }
            objSection.SetDataSet(l_ds);
            objSection.SelectRow(0);
            return objSection;
        }

        //获取子栏目数据
        public Sections CreatorByChildSectionID(int intSectionID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT * FROM [SECTIONS] WHERE intParentID={0} order by intSortNo desc,dtmInTime desc,intSectionID desc ", intSectionID);
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSection = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryByQuerySql(ref l_ds, strSql.ToString()) < 1)
            {
                return null;
            }
            objSection.SetDataSet(l_ds);
            objSection.SelectRow(0);
            return objSection;
        }

        public int UpdateState(int intSectionID)
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            StringBuilder strUpdate = new StringBuilder();
            strUpdate.AppendFormat("UPDATE [ArtSecRelation] SET [INTSTATE]=1 WHERE INTSECTIONID={0} ", intSectionID);
            return l_Access.UpdateSection(strUpdate.ToString());
        }

        /// <summary>
        /// 获取父栏目数据
        /// </summary>
        /// <param name="intSectionID">栏目ID</param>
        /// <returns></returns>
        public Sections CreatorByParentSectionID(int intSectionID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT * From [SECTIONS] Where intSectionID =(SELECT intParentID From [SECTIONS] Where intSectionID={0})  order by intSortNo desc,dtmInTime desc,intSectionID desc", intSectionID);
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSection = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryByQuerySql(ref l_ds, strSql.ToString()) < 1)
            {
                return null;
            }
            objSection.SetDataSet(l_ds);
            objSection.SelectRow(0);
            return objSection;
        }

        /// <summary>
        /// 获取文章所在栏目数据（无栏目内容）
        /// </summary>
        /// <param name="intArticleID">文章ID</param>
        /// <returns></returns>
        public Sections CreatorByArticleID(int intArtSecID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("Select intSectionID,chvSTCName From [Sections] where intSectionID=(Select intSectionID From [ArtSecRelation] where intSecArtID={0}) order by intSortNo desc,dtmInTime desc,intSectionID desc", intArtSecID);
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            Sections objSection = new Sections(this.m_Manager, l_Access);
            DataSet l_ds = new SectionsData();
            if (l_Access.QueryByQuerySql(ref l_ds, strSql.ToString()) < 1)
            {
                return null;
            }
            objSection.SetDataSet(l_ds);
            objSection.SelectRow(0);
            return objSection;
        }

        public int Delete(int intSectionID)
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            return l_Access.Delete(intSectionID);
        }

        public int DeleteWhere(string where)
        {
            SectionsAccess l_Access = new SectionsAccess(m_Manager);
            return l_Access.DeleteWhere(where);
        }
    }
}